package org.example.admin.aircraft;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class ShowAircraftInfoView extends JFrame {
    private JTable table;
    private JButton buttonAdd, buttonAlter, buttonDelete;

    public ShowAircraftInfoView() {
        setTitle("管理端飞机信息查看页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("飞机id");
        model.addColumn("飞机型号");
        model.addColumn("飞机座位");

        // 连接数据库并获取数据
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM air_aircraft");

            while (resultSet.next()) {
                int aircraftId = resultSet.getInt("aircraft_id");
                String aircraftmodel = resultSet.getString("model");
                String seats = resultSet.getString("seats");

                Object[] rowData = {aircraftId, aircraftmodel, seats};
                model.addRow(rowData);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        // 添加刷新按钮
        buttonAdd = new JButton("添加数据");
        buttonAlter = new JButton("修改数据");
        buttonDelete = new JButton("删除数据");
        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        Panel.add(buttonAdd);
        Panel.add(buttonAlter);
        Panel.add(buttonDelete);
        add(Panel, BorderLayout.SOUTH);

        // 添加功能监听器
        buttonAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //页面跳转打开新的窗口,不用关闭原窗口,同时展示
//                dispose();
//                new userAddView();
                SwingUtilities.invokeLater(() -> {
                    addAircraftView addAircraftForm = new addAircraftView();
                    addAircraftForm.setSize(280, 290);
                    addAircraftForm.setVisible(true);
                });
            }
        });
        // 修改功能监听器
        buttonAlter.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //页面跳转打开新的窗口,不用关闭原窗口,同时展示
//                dispose();
                SwingUtilities.invokeLater(() -> {
                    updateAircraftInfoView updateAircraftInfoForm = new updateAircraftInfoView();
                    updateAircraftInfoForm.setSize(380, 300);
                    updateAircraftInfoForm.setVisible(true);
                });
            }
        });
        // 删除功能监听器
        buttonDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //页面跳转打开新的窗口,不用关闭原窗口,同时展示
//                dispose();
                SwingUtilities.invokeLater(() -> {
                    deleteAircraftView deleteAircraftForm = new deleteAircraftView();
                    deleteAircraftForm.setSize(300, 200);
                    deleteAircraftForm.setVisible(true);
                });
            }
        });

    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            ShowAircraftInfoView showAircraftInfoForm = new ShowAircraftInfoView();
//            showAircraftInfoForm.setSize(400, 300);
//            showAircraftInfoForm.setVisible(true);
//        });
    }
}